/*     */ package com.zimbra.cs.account.gal;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GalUtil
/*     */ {
/*     */   public static String expandFilter(String tokenize, String filterTemplate, String key, String token)
/*     */     throws ServiceException
/*     */   {
/*  30 */     return expandFilter(tokenize, filterTemplate, key, token, null);
/*     */   }
/*     */   
/*     */ 
/*     */   public static String expandFilter(String tokenize, String filterTemplate, String key, String token, String extraQuery)
/*     */     throws ServiceException
/*     */   {
/*  37 */     if (key != null) {
/*  38 */       while (key.startsWith("*")) {
/*  39 */         key = key.substring(1);
/*     */       }
/*  41 */       while (key.endsWith("*")) {
/*  42 */         key = key.substring(0, key.length() - 1);
/*     */       }
/*     */     }
/*  45 */     String query = expandKey(tokenize, filterTemplate, key);
/*     */     
/*  47 */     if (query.indexOf("**") > 0) {
/*  48 */       query = query.replaceAll("\\*\\*", "*");
/*     */     }
/*     */     
/*  51 */     if ((token != null) && (token.length() > 0)) {
/*  52 */       String arg = LdapUtil.escapeSearchFilterArg(token);
/*  53 */       query = "(&(|(modifyTimeStamp>=" + arg + ")(createTimeStamp>=" + arg + "))" + query + ")";
/*     */     }
/*     */     
/*  56 */     if (extraQuery != null) {
/*  57 */       query = "(&" + query + extraQuery + ")";
/*     */     }
/*     */     
/*  60 */     return query;
/*     */   }
/*     */   
/*     */   public static String tokenizeKey(GalParams galParams, GalOp galOp) {
/*  64 */     if (galParams == null)
/*  65 */       return null;
/*  66 */     if (galOp == GalOp.autocomplete)
/*  67 */       return galParams.tokenizeAutoCompleteKey();
/*  68 */     if (galOp == GalOp.search) {
/*  69 */       return galParams.tokenizeSearchKey();
/*     */     }
/*  71 */     return null;
/*     */   }
/*     */   
/*     */   private static String expandKey(String tokenize, String filterTemplate, String key) throws ServiceException
/*     */   {
/*  76 */     if (!filterTemplate.startsWith("(")) {
/*  77 */       if (filterTemplate.endsWith(")")) {
/*  78 */         throw ServiceException.INVALID_REQUEST("Unbalanced parenthesis in filter:" + filterTemplate, null);
/*     */       }
/*  80 */       filterTemplate = "(" + filterTemplate + ")";
/*     */     }
/*     */     
/*  83 */     String query = null;
/*  84 */     Map<String, String> vars = new HashMap();
/*     */     
/*  86 */     ZLdapFilterFactory filterFactory = ZLdapFilterFactory.getInstance();
/*     */     
/*  88 */     if (tokenize != null) {
/*  89 */       String[] tokens = key.split("\\s+");
/*  90 */       if (tokens.length > 1) {
/*     */         String q;
/*  92 */         if (GalConstants.TOKENIZE_KEY_AND.equals(tokenize)) {
/*  93 */           q = "(&"; } else { String q;
/*  94 */           if (GalConstants.TOKENIZE_KEY_OR.equals(tokenize)) {
/*  95 */             q = "(|";
/*     */           } else {
/*  97 */             throw ServiceException.FAILURE("invalid attribute value for tokenize key: " + tokenize, null);
/*     */           }
/*     */         }
/* 100 */         for (String t : tokens) {
/* 101 */           vars.clear();
/* 102 */           vars.put("s", filterFactory.encodeValue(t));
/* 103 */           q = q + LdapUtil.expandStr(filterTemplate, vars);
/*     */         }
/* 105 */         String q = q + ")";
/* 106 */         query = q;
/*     */       }
/*     */     }
/*     */     
/* 110 */     if (query == null) {
/* 111 */       vars.put("s", key == null ? null : filterFactory.encodeValue(key));
/* 112 */       query = LdapUtil.expandStr(filterTemplate, vars);
/*     */     }
/*     */     
/* 115 */     return query;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/gal/GalUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */